package com.chixing.controller;

import com.chixing.pojo.Comment;
import com.chixing.pojo.Customer;
import com.chixing.pojo.Product;
import com.chixing.service.GoodService;
import com.chixing.service.RecommendService;
import com.chixing.vo.ProductDetailVO;
import com.chixing.service.ProductService;
import com.chixing.service.ReviewService;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProductController {
  @Autowired
  private ProductService productService;
  @Autowired
  private ReviewService reviewService;
  @Autowired
  private GoodService goodService;
  @Autowired
  private RecommendService recommendService;

  @GetMapping("/pro/kw/{keyword}/page/{pageNum}")
  public ModelAndView getByKeywordAndPage(@PathVariable("keyword") String keyword, @PathVariable("pageNum") Integer pageNum) {
    ServerResult serverResult = productService.getByKeyWord(keyword, pageNum);
    ModelAndView modelAndView = new ModelAndView();
    if (serverResult.getCode() == 200) {
      modelAndView.addObject("keyword", keyword);//将关键字信息绑定到jsp
      modelAndView.addObject("proInfo", serverResult.getData());
      System.out.println(serverResult.getData());
    } else {
      modelAndView.addObject("dateInfo", serverResult.getData());
    }
    System.out.println(serverResult.getData());
    modelAndView.setViewName("commodity_classification/commodityClassification");
    return modelAndView;
  }

  @GetMapping("product/{productId}")
  public ModelAndView getProDetailById(@PathVariable("productId") Integer productId, HttpSession session) {
      ServerResult proDetail = productService.getProDetailById(productId);
      System.out.println("proDetail: " + proDetail.getData());

    //获得商品的介绍信息。
    //ServerResult productInfo = productService.getProductInfo(productId);

    ModelAndView modelAndView = new ModelAndView();
    ProductDetailVO vo = (ProductDetailVO)proDetail.getData();
    //Comment comment=reviewService.getById(productId);
    Customer customer=(Customer) session.getAttribute("loginCustomer");
    int custId=customer.getCustumerId();
    System.out.println("........................................."+custId);
    List<Product> recommendProducts = recommendService.getRecommendProductByProduct(custId, productId, 2);
    modelAndView.addObject("vo", vo);
    modelAndView.addObject("recommendProducts",recommendProducts);
    //商品介绍信息。
    modelAndView.addObject("productInfo",new String(vo.getProduct().getProductIntroduction()));

    modelAndView.setViewName("/product/productDetail");
    return modelAndView;
  }


  @GetMapping("product/{productId}/review")
  public ModelAndView getById2AddReview(@PathVariable("productId") Integer id) {
    ServerResult result = productService.getById2AddReiew(id);
    ModelAndView modelAndView = new ModelAndView();
    ProductDetailVO vo = (ProductDetailVO) result.getData();
    modelAndView.addObject("vo", vo);
    modelAndView.setViewName("/shop/review");
    return modelAndView;
  }


  @ResponseBody
//    @GetMapping("product/{productBrand}/{productSpecs}/{minAge}/{maxAge}/{minPrice}/{maxPrice}/{pageNum}")
  @GetMapping("product/condition")
  public ServerResult getByConditions(@RequestParam(value = "productBrand", required = false) String productBrand,
                                      @RequestParam(value = "minSpecs", required = false) Double minSpecs,
                                      @RequestParam(value = "maxSpecs", required = false) Double maxSpecs,
                                      @RequestParam(value = "productSupportAge", required = false) String productSupportAge,
                                      @RequestParam(value = "minPrice", required = false) Double minPrice,
                                      @RequestParam(value = "maxPrice", required = false) Double maxPrice,
                                      @RequestParam(value = "pageNum", required = false) Integer pageNum) {
    if (pageNum == null) {
      pageNum = 1;
    }
    return productService.getByConditions(productBrand, minSpecs, maxSpecs, productSupportAge, minPrice, maxPrice, pageNum);
  }

  @GetMapping("product/productRecommen")
  @ResponseBody
  public ServerResult getProRecommen() {
    ServerResult recommen = productService.getByRecommen();
    ModelAndView mav = new ModelAndView();
    if (recommen.getCode() == 200) {
      mav.addObject("recommenPro", recommen.getData());
      mav.setViewName("index");

      System.out.println("首页加载的数据" + recommen.getData());
    }
    return recommen;

  }

  //    chc
      /*
       传入结算总价，和用户id
       */
  @GetMapping("buyProduct/str")
  public ModelAndView getbyid(double str, HttpSession session){
    Customer customer=(Customer) session.getAttribute("loginCustomer");
    System.out.println(customer);
    Integer customerId = customer.getCustumerId();

    System.out.println(str);
    System.out.println(customerId);

//        List<Cart>  cartList = cartService.getByCustomerId(customerId);

    ModelAndView mav = new ModelAndView();
    mav.addObject("price",str);
//    mav.addObject("cartList",cartList);
    mav.setViewName("/payOrder/payOrder");
    return mav;
  }

  @PutMapping("/editor/{proId}")
  @ResponseBody
  public ServerResult saveBlobByProId(@PathVariable("proId") Integer proId ,String editorData) {

    ServerResult result = productService.saveBlob(proId, editorData);
    return result;

  }

}

